﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Stamp_For_Africa_Version_2.Models;
using Stamp_For_Africa_Version_2.Repositories;

namespace Stamp_For_Africa_Version_2.Controllers
{
    public class CountriesController : Controller
    {
			readonly CountryRepository _countryRepository = new CountryRepository();

			#region HTTP - GET Methods

			//
			// GET: /Countries/

			public ActionResult Index()
			{

				var artists = _countryRepository.FindAllCountries().ToList();

				return View("Index", artists);
			}

			//
			// GET: /Countries/Details/Id

			public ActionResult Details(Guid id)
			{

				Country country = _countryRepository.GetCountry(id);

				if (country == null)
					return View("NotFound");
				else
					return View("Details", country);
			}

			//
			// GET: /Artists/ArtistCreate

			public ActionResult Create()
			{

				Country country = new Country()
				{

				};

				return View(country);
			}

			//
			// GET: /Artists/ArtistEdit/2

			public ActionResult Edit(Guid id)
			{

				Country country = _countryRepository.GetCountry(id);

				return View(country);
			}


			//
			// GET: /Artists/Delete/xxx

			public ActionResult Delete(Guid id)
			{

				Country country = _countryRepository.GetCountry(id);

				if (country == null)
					return View("NotFound");
				else
					return View(country);
			}

			#endregion

			#region HTTP - POST Methods

			//
			// POST: /Country/Create

			[AcceptVerbs(HttpVerbs.Post)]
			public ActionResult Create(Country country)
			{
				if (ModelState.IsValid)
				{

					try
					{
						country.Id = Guid.NewGuid();
						_countryRepository.Add(country);
						_countryRepository.Save();


						return RedirectToAction("Details", new { id = country.Id });
					}
					catch
					{
						ModelState.AddRuleViolations(country.GetRuleViolations());
					}
				}

				return View(country);
			}

			//
			// POST: /Country/Edit/xxx

			[AcceptVerbs(HttpVerbs.Post)]
			public ActionResult Edit(Guid id, FormCollection formValues)
			{
				Country country = _countryRepository.GetCountry(id);

				try
				{

					UpdateModel(country);
					_countryRepository.Save();

					return RedirectToAction("Details", new { id = country.Id });
				}
				catch
				{
					ModelState.AddRuleViolations(country.GetRuleViolations());

					return View(country);
				}


			}

			//
			// HTTP POST: /Artists/Delete/xxx

			[AcceptVerbs(HttpVerbs.Post)]
			public ActionResult Delete(Guid id, string confirmButton)
			{

				Country country = _countryRepository.GetCountry(id);

				if (country == null)
					return View("NotFound");

				_countryRepository.Delete(country);
				_countryRepository.Save();

				return View("Deleted");
			}

			#endregion

    }
}
